
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	/**
	 * 上传
	 */
	exports.ldh_upload = function(id,path,fn) {
		
		$("#"+id).ldh_upload_file({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				x_id : "x_"+id,
				y_id : "y_"+id,
				width_id : "width_"+id,
				height_id : "height_"+id,
				rename : "true",
				path : path,
				successCallBack : fn
			}

		});
	};

	$.fn.ldh_upload_file = function(opts) {

			//初始化上传控件
			function init(){
				if($("#_upload_frame_"+opts.options.form_id).length==0){
					$(document.body).append('<iframe style="display:none;" class="opacitystyle" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
				}
				var htmlStr="";
				htmlStr += '<form class="ops_post" id="'+opts.options.form_id+'" method="post" enctype="multipart/form-data">';
				htmlStr += '<button class="btnFile"  type="button" >选择图片</button>  <input class="opacity_0" name="'+opts.options.input_id+'" id="'+opts.options.input_id+'" type="file" value="上传头像" /> ';
				htmlStr += '<input type="hidden" name="x" id="'+opts.options.x_id+'" /><input type="hidden" name="y" id="'+opts.options.y_id+'" /><input name="width" type="hidden" id="'+opts.options.height_id+'" /><input name="height" type="hidden" id="'+opts.options.width_id+'" /></form>';
				
               
				$("#"+opts.options.div_id).html(htmlStr);
				$("#"+opts.options.input_id).change(function(){
					if(!CheckUploadFile(opts.options.input_id))
						return;
					
					$("#upload_loading_"+opts.options.div_id).show();
					var frame_url=window.location.protocol +"//" + window.location.host+ctx+"/frame.jsp?callBack="+opts.options.successCallBack;
					document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;
					document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
				
					document.getElementById(opts.options.form_id).submit();
					

					
				});
				
			}
			
			init();
	}
	
	exports.ldh_upload_store = function(id,path,fn) {
		
		$("#"+id).ldh_upload_file_store({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				successCallBack : fn
			}

		});
	};
	
	$.fn.ldh_upload_file_store = function(opts) {

		//初始化上传控件
		function init(){
			if($("#_upload_frame_"+opts.options.form_id).length==0){
				$(document.body).append('<iframe style="display:none;" class="opacitystyle" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
			}
			var htmlStr="";
			htmlStr += '<form class="ops_post" id="'+opts.options.form_id+'" method="post" enctype="multipart/form-data">';
			htmlStr += '<button class="btnFile"  type="button" >选择图片</button>  <input class="opacity_0" name="'+opts.options.input_id+'" id="'+opts.options.input_id+'" type="file" value="上传头像" /> ';
			htmlStr += '</form>';
			
           
			$("#"+opts.options.div_id).html(htmlStr);
			$("#"+opts.options.input_id).change(function(){
				if(!CheckUploadFile(opts.options.input_id))
					return;
				
				$("#"+opts.options.div_id+"_img").css("background-image","url("+ctx+"/v1/img/upload_loading.gif)");
				var frame_url=window.location.protocol +"//" + window.location.host+ctx+"/frame.jsp?callBack="+opts.options.successCallBack;
				document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;
				document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
			
				document.getElementById(opts.options.form_id).submit();
				

				
			});
			
		}
		
		init();
}
	

	function CheckUploadFile(id) {
		
		var tmp = document.getElementById(id).value;
		if (tmp != '') {
			tmp = tmp.toLowerCase();
			var patten = /(.jpg|.png|.gif)$/;
			if (tmp.search(patten) <= -1) {
				base.tips_error('只允许上传 jpeg/gif/png 格式的文件');
				return false;
			}
		}

		return true;
	}
	
	
	/**
	 * 上传商家相册
	 */
	exports.ldh_upload_shopPhots = function(id,path,fn) {
		$("#"+id).ldh_upload_image({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				successCallBack : fn
			}

		});
	};
	
	$.fn.ldh_upload_image = function(opts) {

		//初始化上传控件
		function init(){
			

			if($("#_upload_frame_"+opts.options.form_id).length==0){
				$(document.body).append('<iframe class="opacitystyle" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
			}
			var htmlStr="";
			htmlStr += '<form id="'+opts.options.form_id+'" style="display:none" method="post" enctype="multipart/form-data">';
			htmlStr += '<div class="uploader" id="uniform-undefined"><input type="file" name="myfile" id="'+opts.options.input_id+'" size="19" style="opacity: 0;"><span class="filename"></span><span class="action">选择文件</span></div>';
			htmlStr += '</form>';
			$("#divLink"+opts.options.div_id).html(htmlStr);
			
			$("#"+opts.options.input_id).click();	
			
			$("#"+opts.options.input_id).change(function(){
				if(!CheckUploadFile(opts.options.input_id))
					return;
				base.showLoading();
				var frame_url=window.location.protocol +"//" + window.location.host+ctx+"/admin/frame.jsp?callBack="+opts.options.successCallBack;
				document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;

				document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
				
				document.getElementById(opts.options.form_id).submit();
				
				
			});
			
		}
		
		init();
	};
	
	/**
	 * 上传取消原因图片
	 */
	exports.ldh_upload_reasonPhots = function(id,path,fn,allowNum) {
		$("#"+id).ldh_upload_reason({
			options : {
				url : fileUploadUrl,
				div_id : id,
				form_id : "form_"+id,
				input_id : "input_"+id,
				rename : "true",
				path : path,
				allowNum : allowNum,
				successCallBack : fn
			}

		});
	};
	
	$.fn.ldh_upload_reason = function(opts) {

		//初始化上传控件
		function init(){
			
			
			if($("#_upload_frame_"+opts.options.form_id).length==0){
				$(document.body).append('<iframe class="opacitystyle" id="_upload_frame_'+opts.options.form_id+'" name="_upload_frame_'+opts.options.form_id+'"></iframe>');
			}
			var htmlStr="";
			htmlStr += '<form id="'+opts.options.form_id+'" style="display:none" method="post" enctype="multipart/form-data">';
			htmlStr += '<div class="uploader" id="uniform-undefined"><input type="file" name="myfile" id="'+opts.options.input_id+'" size="19" style="opacity: 0;" multiple="multiple"><span class="filename"></span><span class="action">选择文件</span></div>';
			htmlStr += '</form>';
			$("#"+opts.options.div_id).html(htmlStr);
			$("#"+opts.options.input_id).click();	
			$("#"+opts.options.input_id).change(function(){
				if(!CheckUploadFile(opts.options.input_id))
					return;
				if(!fileCountCheck(opts.options.input_id,opts.options.allowNum))
					return;
				
				base.showLoading();
				var frame_url=window.location.protocol +"//" + window.location.host+ctx+"/admin/frame.jsp?callBack="+opts.options.successCallBack;
				
				document.getElementById(opts.options.form_id).action=opts.options.url+"?path="+opts.options.path+"&rename=true&overwrite=true&url="+frame_url;

				document.getElementById(opts.options.form_id).target="_upload_frame_"+opts.options.form_id;
				
				document.getElementById(opts.options.form_id).submit();
				
				
			});
			
		}
		
		init();
	};
	
	function fileCountCheck(_id,_allowNum){  
		var num=0;
		$.each($('#'+_id).prop("files"), function(k, v) {
		    num++;
		});
		
		if(num>_allowNum){
			base.tips_error('您上传的文件总个数已经超过允许上传总个数，请重新选择！');
			return false;
		}
		return true;                     
	}
	
});